/*     */ package com.zimbra.qa.unittest.prov.soap;
/*     */ 
/*     */ import com.google.common.collect.Maps;
/*     */ import com.google.common.collect.Sets;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.account.ZAttrProvisioning.AutoProvAuthMech;
/*     */ import com.zimbra.common.account.ZAttrProvisioning.AutoProvMode;
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.SoapHttpTransport;
/*     */ import com.zimbra.common.soap.SoapTransport;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.PreAuthKey;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.auth.AuthMechanism.AuthMech;
/*     */ import com.zimbra.cs.ldap.LdapUtil;
/*     */ import com.zimbra.qa.QA.Bug;
/*     */ import com.zimbra.qa.unittest.TestPreAuthServlet;
/*     */ import com.zimbra.qa.unittest.TestUtil;
/*     */ import com.zimbra.qa.unittest.prov.AutoProvisionTestUtil;
/*     */ import com.zimbra.qa.unittest.prov.AutoProvisionTestUtil.MarkEntryProvisionedListener;
/*     */ import com.zimbra.qa.unittest.prov.Verify;
/*     */ import com.zimbra.soap.admin.message.AutoProvAccountRequest;
/*     */ import com.zimbra.soap.admin.message.AutoProvAccountResponse;
/*     */ import com.zimbra.soap.admin.message.AutoProvTaskControlRequest;
/*     */ import com.zimbra.soap.admin.message.AutoProvTaskControlRequest.Action;
/*     */ import com.zimbra.soap.admin.message.AutoProvTaskControlResponse;
/*     */ import com.zimbra.soap.admin.message.AutoProvTaskControlResponse.Status;
/*     */ import com.zimbra.soap.admin.type.AccountInfo;
/*     */ import com.zimbra.soap.admin.type.CountObjectsType;
/*     */ import com.zimbra.soap.admin.type.DomainSelector;
/*     */ import com.zimbra.soap.admin.type.DomainSelector.DomainBy;
/*     */ import com.zimbra.soap.admin.type.PrincipalSelector;
/*     */ import com.zimbra.soap.type.AutoProvPrincipalBy;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.HttpException;
/*     */ import org.apache.commons.httpclient.HttpMethod;
/*     */ import org.apache.commons.httpclient.StatusLine;
/*     */ import org.apache.commons.httpclient.methods.GetMethod;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Ignore;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestAutoProvision
/*     */   extends SoapTest
/*     */ {
/*     */   private static SoapProvTestUtil provUtil;
/*     */   private static Provisioning prov;
/*     */   private static Domain extDomain;
/*     */   private static String extDomainDn;
/*  81 */   private static String extDomainAdminBindDn = LC.zimbra_ldap_userdn.value();
/*  82 */   private static String extDomainAdminBindPassword = LC.zimbra_ldap_password.value();
/*     */   
/*  84 */   private static String DEFAULT_AUTOPROV_INITIAL_SLEEP_MS = String.valueOf(300000L);
/*  85 */   private static String DEFAULT_AUTOPROV_POLLING_INTERVAL = "15m";
/*     */   
/*     */   @BeforeClass
/*     */   public static void init() throws Exception {
/*  89 */     provUtil = new SoapProvTestUtil();
/*  90 */     prov = provUtil.getProv();
/*  91 */     extDomain = provUtil.createDomain("external." + baseDomainName());
/*  92 */     extDomainDn = LdapUtil.domainToDN(extDomain.getName());
/*     */     
/*  94 */     revertAllToDefault();
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void cleanup() throws Exception {
/*  99 */     Cleanup.deleteAll(new String[] { baseDomainName() });
/* 100 */     revertAllToDefault();
/*     */   }
/*     */   
/*     */   private static void revertAllToDefault()
/*     */     throws Exception
/*     */   {
/* 106 */     SoapTransport transport = authZimbraAdmin();
/* 107 */     modifyLocalconfigAndReload(transport, LC.autoprov_initial_sleep_ms, DEFAULT_AUTOPROV_INITIAL_SLEEP_MS);
/*     */     
/* 109 */     Server localServer = prov.getLocalServer();
/* 110 */     localServer.setAutoProvPollingInterval(DEFAULT_AUTOPROV_POLLING_INTERVAL);
/* 111 */     localServer.unsetAutoProvScheduledDomains();
/*     */   }
/*     */   
/*     */   private String getZimbraDomainName(String testName) {
/* 115 */     return testName + "." + baseDomainName();
/*     */   }
/*     */   
/*     */   private Domain createZimbraDomain(String testName, Map<String, Object> zimbraDomainAttrs) throws Exception
/*     */   {
/* 120 */     return provUtil.createDomain(getZimbraDomainName(testName), zimbraDomainAttrs);
/*     */   }
/*     */   
/*     */   private String createExternalAcctEntry(String localPart) throws Exception {
/* 124 */     return createExternalAcctEntry(localPart, null);
/*     */   }
/*     */   
/*     */   private String createExternalAcctEntry(String localPart, Map<String, Object> attrs) throws Exception
/*     */   {
/* 129 */     return createExternalAcctEntry(localPart, null, attrs);
/*     */   }
/*     */   
/*     */   private String createExternalAcctEntry(String localPart, String externalPassword, Map<String, Object> attrs) throws Exception
/*     */   {
/* 134 */     String extAcctName = TestUtil.getAddress(localPart, extDomain.getName());
/*     */     
/* 136 */     Map<String, Object> extAcctAttrs = attrs == null ? new HashMap() : attrs;
/*     */     
/* 138 */     extAcctAttrs.put("displayName", "display name");
/* 139 */     extAcctAttrs.put("sn", "last name");
/* 140 */     Account extAcct = prov.createAccount(extAcctName, externalPassword, extAcctAttrs);
/* 141 */     return extAcctName;
/*     */   }
/*     */   
/*     */   private Map<String, Object> commonZimbraDomainAttrs() {
/* 145 */     return AutoProvisionTestUtil.commonZimbraDomainAttrs();
/*     */   }
/*     */   
/*     */   private void verifyAcctAutoProvisioned(Account acct, String expectedAcctName) throws Exception
/*     */   {
/* 150 */     AutoProvisionTestUtil.verifyAcctAutoProvisioned(acct, expectedAcctName);
/*     */   }
/*     */   
/*     */   private String getAuthTokenAcctId(String authToken) throws Exception
/*     */   {
/* 155 */     Map attrs = AuthToken.getInfo(authToken);
/* 156 */     String zimbraId = (String)attrs.get("id");
/* 157 */     Assert.assertNotNull(zimbraId);
/* 158 */     return zimbraId;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   @Test
/*     */   public void authRequestByPassword()
/*     */     throws Exception
/*     */   {
/* 173 */     String testName = getTestName();
/*     */     
/* 175 */     String externalPassword = "test456";
/* 176 */     String extAcctLocalPart = testName;
/* 177 */     String extAcctName = createExternalAcctEntry(extAcctLocalPart, externalPassword, null);
/*     */     
/* 179 */     Map<String, Object> zimbraDomainAttrs = commonZimbraDomainAttrs();
/*     */     
/* 181 */     zimbraDomainAttrs.put("zimbraAutoProvLdapSearchBase", extDomainDn);
/* 182 */     zimbraDomainAttrs.put("zimbraAutoProvLdapSearchFilter", "(uid=%u)");
/*     */     
/* 184 */     zimbraDomainAttrs.put("zimbraAuthMech", AuthMechanism.AuthMech.ldap.name());
/* 185 */     zimbraDomainAttrs.put("zimbraAuthLdapURL", "ldap://localhost:389");
/* 186 */     zimbraDomainAttrs.put("zimbraAuthLdapBindDn", "uid=%u,ou=people," + extDomainDn);
/* 187 */     Domain zimbraDomain = createZimbraDomain(testName, zimbraDomainAttrs);
/*     */     
/* 189 */     String loginName = extAcctLocalPart + "@" + zimbraDomain.getName();
/*     */     
/*     */ 
/* 192 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getSoapUrl());
/*     */     
/* 194 */     Element request = Element.create(transport.getRequestProtocol(), AccountConstants.AUTH_REQUEST);
/* 195 */     request.addElement("account").addAttribute("by", Key.AccountBy.name.name()).setText(loginName);
/* 196 */     request.addElement("password").setText(externalPassword);
/*     */     
/* 198 */     Element response = transport.invoke(request);
/*     */     
/* 200 */     String encodedAuthToken = response.getElement("authToken").getText();
/* 201 */     Assert.assertNotNull(encodedAuthToken);
/* 202 */     String acctId = getAuthTokenAcctId(encodedAuthToken);
/* 203 */     Account acct = prov.get(Key.AccountBy.id, acctId);
/* 204 */     verifyAcctAutoProvisioned(acct, loginName.toLowerCase());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   @Test
/*     */   public void authRequestByPreauth()
/*     */     throws Exception
/*     */   {
/* 214 */     String testName = getTestName();
/*     */     
/* 216 */     String externalPassword = "test456";
/* 217 */     String extAcctLocalPart = testName;
/* 218 */     String extAcctName = createExternalAcctEntry(extAcctLocalPart, externalPassword, null);
/*     */     
/* 220 */     Map<String, Object> zimbraDomainAttrs = commonZimbraDomainAttrs();
/*     */     
/*     */ 
/* 223 */     StringUtil.addToMultiMap(zimbraDomainAttrs, "zimbraAutoProvAuthMech", ZAttrProvisioning.AutoProvAuthMech.PREAUTH.name());
/* 224 */     zimbraDomainAttrs.put("zimbraAutoProvLdapSearchBase", extDomainDn);
/* 225 */     zimbraDomainAttrs.put("zimbraAutoProvLdapSearchFilter", "(uid=%u)");
/*     */     
/* 227 */     zimbraDomainAttrs.put("zimbraAuthMech", AuthMechanism.AuthMech.ldap.name());
/* 228 */     zimbraDomainAttrs.put("zimbraAuthLdapURL", "ldap://localhost:389");
/* 229 */     zimbraDomainAttrs.put("zimbraAuthLdapBindDn", "uid=%u,ou=people," + extDomainDn);
/*     */     
/* 231 */     String preAuthKey = PreAuthKey.generateRandomPreAuthKey();
/* 232 */     zimbraDomainAttrs.put("zimbraPreAuthKey", preAuthKey);
/*     */     
/* 234 */     Domain zimbraDomain = createZimbraDomain(testName, zimbraDomainAttrs);
/*     */     
/* 236 */     String loginName = extAcctLocalPart + "@" + zimbraDomain.getName();
/*     */     
/*     */ 
/* 239 */     HashMap<String, String> params = new HashMap();
/* 240 */     String authBy = "name";
/* 241 */     long timestamp = System.currentTimeMillis();
/* 242 */     long expires = 0L;
/*     */     
/* 244 */     params.put("account", loginName);
/* 245 */     params.put("by", authBy);
/* 246 */     params.put("timestamp", timestamp + "");
/* 247 */     params.put("expires", expires + "");
/*     */     
/* 249 */     String preAuth = PreAuthKey.computePreAuth(params, preAuthKey);
/*     */     
/*     */ 
/*     */ 
/* 253 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getSoapUrl());
/*     */     
/* 255 */     Element request = Element.create(transport.getRequestProtocol(), AccountConstants.AUTH_REQUEST);
/* 256 */     request.addElement("account").addAttribute("by", authBy).setText(loginName);
/* 257 */     Element ePreAuth = request.addElement("preauth").setText(preAuth);
/* 258 */     ePreAuth.addAttribute("timestamp", timestamp);
/* 259 */     ePreAuth.addAttribute("expires", expires);
/*     */     
/* 261 */     Element response = transport.invoke(request);
/*     */     
/* 263 */     String encodedAuthToken = response.getElement("authToken").getText();
/* 264 */     Assert.assertNotNull(encodedAuthToken);
/* 265 */     String acctId = getAuthTokenAcctId(encodedAuthToken);
/* 266 */     Account acct = prov.get(Key.AccountBy.id, acctId);
/* 267 */     verifyAcctAutoProvisioned(acct, loginName.toLowerCase());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   @Test
/*     */   @Ignore
/*     */   public void authRequestByKrb5()
/*     */     throws Exception
/*     */   {
/* 277 */     String testName = getTestName();
/*     */     
/* 279 */     String externalPassword = "test456";
/* 280 */     String extAcctLocalPart = testName;
/* 281 */     String extAcctName = createExternalAcctEntry(extAcctLocalPart, externalPassword, null);
/*     */     
/* 283 */     String krb5Realm = "MYREALM";
/*     */     
/* 285 */     Map<String, Object> zimbraDomainAttrs = commonZimbraDomainAttrs();
/*     */     
/* 287 */     StringUtil.addToMultiMap(zimbraDomainAttrs, "zimbraAutoProvAuthMech", ZAttrProvisioning.AutoProvAuthMech.KRB5.name());
/* 288 */     zimbraDomainAttrs.put("zimbraAutoProvLdapSearchBase", extDomainDn);
/* 289 */     zimbraDomainAttrs.put("zimbraAutoProvLdapSearchFilter", "(uid=%u)");
/*     */     
/* 291 */     zimbraDomainAttrs.put("zimbraAuthMech", AuthMechanism.AuthMech.kerberos5.name());
/* 292 */     zimbraDomainAttrs.put("zimbraAuthKerberos5Realm", krb5Realm);
/* 293 */     zimbraDomainAttrs.put("zimbraAuthKerberos5Realm", krb5Realm);
/* 294 */     Domain zimbraDomain = createZimbraDomain(testName, zimbraDomainAttrs);
/*     */     
/* 296 */     String loginName = extAcctLocalPart + "@" + krb5Realm;
/*     */     
/*     */ 
/* 299 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getSoapUrl());
/*     */     
/* 301 */     Element request = Element.create(transport.getRequestProtocol(), AccountConstants.AUTH_REQUEST);
/* 302 */     request.addElement("account").addAttribute("by", Key.AccountBy.krb5Principal.name()).setText(loginName);
/* 303 */     request.addElement("password").setText(externalPassword);
/*     */     
/* 305 */     Element response = transport.invoke(request);
/*     */     
/* 307 */     String encodedAuthToken = response.getElement("authToken").getText();
/* 308 */     Assert.assertNotNull(encodedAuthToken);
/* 309 */     String acctId = getAuthTokenAcctId(encodedAuthToken);
/* 310 */     Account acct = prov.get(Key.AccountBy.id, acctId);
/* 311 */     verifyAcctAutoProvisioned(acct, loginName.toLowerCase());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   @Test
/*     */   public void preauthServlet()
/*     */     throws Exception
/*     */   {
/* 320 */     String testName = getTestName();
/*     */     
/* 322 */     String externalPassword = "test456";
/* 323 */     String extAcctLocalPart = testName;
/* 324 */     String extAcctName = createExternalAcctEntry(extAcctLocalPart, externalPassword, null);
/*     */     
/* 326 */     Map<String, Object> zimbraDomainAttrs = commonZimbraDomainAttrs();
/*     */     
/*     */ 
/* 329 */     StringUtil.addToMultiMap(zimbraDomainAttrs, "zimbraAutoProvAuthMech", ZAttrProvisioning.AutoProvAuthMech.PREAUTH.name());
/* 330 */     zimbraDomainAttrs.put("zimbraAutoProvLdapSearchBase", extDomainDn);
/* 331 */     zimbraDomainAttrs.put("zimbraAutoProvLdapSearchFilter", "(uid=%u)");
/*     */     
/* 333 */     zimbraDomainAttrs.put("zimbraAuthMech", AuthMechanism.AuthMech.ldap.name());
/* 334 */     zimbraDomainAttrs.put("zimbraAuthLdapURL", "ldap://localhost:389");
/* 335 */     zimbraDomainAttrs.put("zimbraAuthLdapBindDn", "uid=%u,ou=people," + extDomainDn);
/*     */     
/* 337 */     String preAuthKey = PreAuthKey.generateRandomPreAuthKey();
/* 338 */     zimbraDomainAttrs.put("zimbraPreAuthKey", preAuthKey);
/*     */     
/* 340 */     Domain zimbraDomain = createZimbraDomain(testName, zimbraDomainAttrs);
/*     */     
/* 342 */     String loginName = extAcctLocalPart + "@" + zimbraDomain.getName();
/*     */     
/*     */ 
/* 345 */     String preAuthUrl = TestPreAuthServlet.genPreAuthUrl(preAuthKey, loginName, false, false);
/*     */     
/*     */ 
/* 348 */     String url = TestUtil.getBaseUrl() + preAuthUrl;
/*     */     
/* 350 */     HttpClient client = new HttpClient();
/* 351 */     HttpMethod method = new GetMethod(url);
/*     */     
/* 353 */     boolean ok = false;
/*     */     try {
/* 355 */       int respCode = HttpClientUtil.executeMethod(client, method);
/* 356 */       int statusCode = method.getStatusCode();
/* 357 */       String statusLine = method.getStatusLine().toString();
/*     */       
/* 359 */       ok = respCode == 200;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     }
/*     */     catch (HttpException e)
/*     */     {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 380 */       throw e;
/*     */     } catch (IOException e) {
/* 382 */       throw e;
/*     */     } finally {
/* 384 */       method.releaseConnection();
/*     */     }
/*     */     
/* 387 */     Assert.assertTrue(ok);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void verifyAutoProvTask(SoapTransport transport, AutoProvTaskControlRequest.Action action, AutoProvTaskControlResponse.Status expectedSatus)
/*     */     throws Exception
/*     */   {
/* 402 */     AutoProvTaskControlRequest req = new AutoProvTaskControlRequest(action);
/*     */     
/* 404 */     AutoProvTaskControlResponse resp = (AutoProvTaskControlResponse)invokeJaxb(transport, req);
/* 405 */     AutoProvTaskControlResponse.Status status = resp.getStatus();
/*     */     
/* 407 */     Assert.assertEquals(expectedSatus.name(), status.name());
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void attributeCallbackAutoProvScheduledDomains() throws Exception {
/* 412 */     Domain domain1 = createZimbraDomain(genDomainSegmentName("1"), null);
/* 413 */     domain1.setAutoProvMode(ZAttrProvisioning.AutoProvMode.EAGER);
/*     */     
/* 415 */     Domain domain2 = createZimbraDomain(genDomainSegmentName("2"), null);
/* 416 */     domain2.setAutoProvMode(ZAttrProvisioning.AutoProvMode.EAGER);
/*     */     
/* 418 */     Account admin = provUtil.createGlobalAdmin(genAcctNameLocalPart(), domain1);
/* 419 */     SoapTransport transport = authAdmin(admin.getName());
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 424 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.status, AutoProvTaskControlResponse.Status.idle);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 429 */     Server localServer = prov.getLocalServer();
/* 430 */     localServer.setAutoProvScheduledDomains(new String[] { domain1.getName(), domain2.getName() });
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 435 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.status, AutoProvTaskControlResponse.Status.running);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 440 */     localServer.removeAutoProvScheduledDomains(domain1.getName());
/* 441 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.status, AutoProvTaskControlResponse.Status.running);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 447 */     localServer.removeAutoProvScheduledDomains(domain2.getName());
/* 448 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.status, AutoProvTaskControlResponse.Status.idle);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 453 */     Domain domain3 = createZimbraDomain(genDomainSegmentName("3"), null);
/* 454 */     domain3.setAutoProvMode(ZAttrProvisioning.AutoProvMode.MANUAL);
/* 455 */     String exceptionCaught = null;
/*     */     try {
/* 457 */       localServer.addAutoProvScheduledDomains(domain3.getName());
/*     */     } catch (ServiceException e) {
/* 459 */       exceptionCaught = e.getCode();
/*     */     }
/* 461 */     Assert.assertEquals("service.INVALID_REQUEST", exceptionCaught);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void nonEagerDomainRemovedFromScheduledDomains() throws Exception {
/* 466 */     Domain domain1 = createZimbraDomain(genDomainSegmentName("1"), null);
/* 467 */     domain1.setAutoProvMode(ZAttrProvisioning.AutoProvMode.EAGER);
/*     */     
/* 469 */     Domain domain2 = createZimbraDomain(genDomainSegmentName("2"), null);
/* 470 */     domain2.setAutoProvMode(ZAttrProvisioning.AutoProvMode.EAGER);
/*     */     
/* 472 */     Account admin = provUtil.createGlobalAdmin(genAcctNameLocalPart(), domain1);
/* 473 */     SoapTransport transport = authAdmin(admin.getName());
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 478 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.status, AutoProvTaskControlResponse.Status.idle);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 484 */     modifyLocalconfigAndReload(transport, LC.autoprov_initial_sleep_ms, "0");
/* 485 */     Server localServer = prov.getLocalServer();
/* 486 */     long sleepInterval = 3000L;
/* 487 */     localServer.setAutoProvPollingInterval("3s");
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 492 */     localServer.setAutoProvScheduledDomains(new String[] { domain1.getName(), domain2.getName() });
/* 493 */     Verify.verifyEquals(Sets.newHashSet(new String[] { domain1.getName(), domain2.getName() }), localServer.getAutoProvScheduledDomains());
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 499 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.status, AutoProvTaskControlResponse.Status.running);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 506 */     domain1.setAutoProvMode(ZAttrProvisioning.AutoProvMode.MANUAL);
/* 507 */     Thread.sleep(2L * sleepInterval);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 512 */     prov.reload(localServer);
/* 513 */     Verify.verifyEquals(Sets.newHashSet(new String[] { domain2.getName() }), localServer.getAutoProvScheduledDomains());
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 518 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.status, AutoProvTaskControlResponse.Status.running);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 526 */     domain2.setAutoProvMode(ZAttrProvisioning.AutoProvMode.MANUAL);
/* 527 */     Thread.sleep(2L * sleepInterval);
/* 528 */     prov.reload(localServer);
/* 529 */     Verify.verifyEquals(new HashSet(), localServer.getAutoProvScheduledDomains());
/* 530 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.status, AutoProvTaskControlResponse.Status.idle);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 535 */     modifyLocalconfigAndReload(transport, LC.autoprov_initial_sleep_ms, DEFAULT_AUTOPROV_INITIAL_SLEEP_MS);
/* 536 */     localServer.setAutoProvPollingInterval(DEFAULT_AUTOPROV_POLLING_INTERVAL);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void attributeCallbackAutoProvPollingInterval() throws Exception {
/* 541 */     Domain domain1 = createZimbraDomain(genDomainSegmentName("1"), null);
/* 542 */     domain1.setAutoProvMode(ZAttrProvisioning.AutoProvMode.EAGER);
/*     */     
/* 544 */     Domain domain2 = createZimbraDomain(genDomainSegmentName("2"), null);
/* 545 */     domain2.setAutoProvMode(ZAttrProvisioning.AutoProvMode.EAGER);
/*     */     
/* 547 */     Account admin = provUtil.createGlobalAdmin(genAcctNameLocalPart(), domain1);
/* 548 */     SoapTransport transport = authAdmin(admin.getName());
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 553 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.status, AutoProvTaskControlResponse.Status.idle);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 558 */     Server localServer = prov.getLocalServer();
/* 559 */     localServer.setAutoProvScheduledDomains(new String[] { domain1.getName(), domain2.getName() });
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 564 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.status, AutoProvTaskControlResponse.Status.running);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 571 */     String curPollingInterval = localServer.getAutoProvPollingIntervalAsString();
/* 572 */     localServer.setAutoProvPollingInterval("0");
/* 573 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.status, AutoProvTaskControlResponse.Status.idle);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 578 */     localServer.unsetAutoProvScheduledDomains();
/* 579 */     localServer.setAutoProvPollingInterval(curPollingInterval);
/* 580 */     Verify.verifyEquals(new HashSet(), localServer.getAutoProvScheduledDomains());
/* 581 */     Assert.assertEquals(curPollingInterval, localServer.getAutoProvPollingIntervalAsString());
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void autoProvTaskControl() throws Exception {
/* 586 */     Domain domain = createZimbraDomain(genDomainSegmentName(), null);
/* 587 */     Account admin = provUtil.createGlobalAdmin(genAcctNameLocalPart(), domain);
/* 588 */     SoapTransport transport = authAdmin(admin.getName());
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 593 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.status, AutoProvTaskControlResponse.Status.idle);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 598 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.start, AutoProvTaskControlResponse.Status.started);
/* 599 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.status, AutoProvTaskControlResponse.Status.running);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 604 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.stop, AutoProvTaskControlResponse.Status.stopped);
/* 605 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.status, AutoProvTaskControlResponse.Status.idle);
/*     */   }
/*     */   
/*     */   public static class TestDomainLockListener extends AutoProvisionTestUtil.MarkEntryProvisionedListener
/*     */   {
/*     */     private static final long LONGTIME = 86400000L;
/*     */     private static final int HOLD_IT_AT_THIS_ENTRY = 2;
/* 612 */     private int numEntriesAutoProvisioned = 0;
/*     */     
/*     */     public void postCreate(Domain domain, Account acct, String externalDN)
/*     */     {
/* 616 */       super.postCreate(domain, acct, externalDN);
/*     */       
/* 618 */       this.numEntriesAutoProvisioned += 1;
/* 619 */       if (2 == this.numEntriesAutoProvisioned)
/*     */       {
/*     */ 
/*     */         try
/*     */         {
/*     */ 
/* 625 */           Thread.sleep(86400000L);
/*     */         }
/*     */         catch (InterruptedException e) {
/* 628 */           e.printStackTrace();
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void eagerModeDomainUnlockedWhenThreadStopped() throws Exception
/*     */   {
/* 637 */     int numAccts = 4;
/*     */     
/* 639 */     for (int i = 1; i <= numAccts; i++) {
/* 640 */       createExternalAcctEntry("eagerMode-" + i, "test123", null);
/*     */     }
/*     */     
/* 643 */     Map<String, Object> zimbraDomainAttrs = AutoProvisionTestUtil.commonZimbraDomainAttrs();
/*     */     
/* 645 */     zimbraDomainAttrs.put("zimbraAutoProvLdapSearchBase", extDomainDn);
/* 646 */     zimbraDomainAttrs.put("zimbraAutoProvLdapSearchFilter", "(&(uid=%u)(mail=eagerMode*)(!(zimbraNotes=PROVISIONED IN ZIMBRA)))");
/*     */     
/* 648 */     zimbraDomainAttrs.put("zimbraAutoProvAccountNameMap", "uid");
/*     */     
/* 650 */     zimbraDomainAttrs.put("zimbraAutoProvListenerClass", TestDomainLockListener.class.getName());
/*     */     
/* 652 */     Domain zimbraDomain = createZimbraDomain(genDomainSegmentName(), zimbraDomainAttrs);
/*     */     
/*     */ 
/* 655 */     Domain domain = provUtil.createDomain(getZimbraDomainName("admin-domain"));
/* 656 */     Account admin = provUtil.createGlobalAdmin(genAcctNameLocalPart(), domain);
/* 657 */     SoapTransport transport = authAdmin(admin.getName());
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 662 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.status, AutoProvTaskControlResponse.Status.idle);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 668 */     modifyLocalconfigAndReload(transport, LC.autoprov_initial_sleep_ms, "0");
/*     */     
/*     */ 
/*     */ 
/* 672 */     Server localServer = prov.getLocalServer();
/* 673 */     localServer.addAutoProvScheduledDomains(zimbraDomain.getName());
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 679 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.status, AutoProvTaskControlResponse.Status.running);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */     for (;;)
/*     */     {
/* 686 */       long numAcctsAutoProvisioned = prov.countObjects(CountObjectsType.account, zimbraDomain, null);
/* 687 */       if (numAcctsAutoProvisioned == 2L) {
/*     */         break;
/*     */       }
/* 690 */       System.out.println(getTestName() + " waiting for 1 second");
/* 691 */       Thread.sleep(1000L);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 698 */     prov.reload(zimbraDomain);
/* 699 */     Assert.assertEquals(localServer.getId(), zimbraDomain.getAutoProvLock());
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 705 */     localServer.unsetAutoProvScheduledDomains();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 710 */     verifyAutoProvTask(transport, AutoProvTaskControlRequest.Action.status, AutoProvTaskControlResponse.Status.idle);
/* 711 */     prov.reload(zimbraDomain);
/* 712 */     Assert.assertNull(zimbraDomain.getAutoProvLock());
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 717 */     modifyLocalconfigAndReload(transport, LC.autoprov_initial_sleep_ms, DEFAULT_AUTOPROV_INITIAL_SLEEP_MS);
/*     */   }
/*     */   
/*     */ 
/*     */   @Test
/*     */   @QA.Bug(bug={70720})
/*     */   public void errorHandling()
/*     */     throws Exception
/*     */   {
/* 726 */     Map<String, Object> domainAttrs = Maps.newHashMap();
/* 727 */     domainAttrs.put("zimbraAutoProvLdapURL", "ldap://localhost:389");
/* 728 */     domainAttrs.put("zimbraAutoProvLdapAdminBindDn", extDomainAdminBindDn);
/* 729 */     domainAttrs.put("zimbraAutoProvLdapAdminBindPassword", extDomainAdminBindPassword);
/* 730 */     StringUtil.addToMultiMap(domainAttrs, "zimbraAutoProvMode", ZAttrProvisioning.AutoProvMode.LAZY.name());
/* 731 */     StringUtil.addToMultiMap(domainAttrs, "zimbraAutoProvMode", ZAttrProvisioning.AutoProvMode.MANUAL.name());
/* 732 */     domainAttrs.put("zimbraAutoProvLdapSearchFilter", "(cn=auth*)");
/*     */     
/* 734 */     domainAttrs.put("zimbraAutoProvLdapSearchBase", extDomainDn);
/* 735 */     domainAttrs.put("zimbraAutoProvAccountNameMap", "cn");
/* 736 */     domainAttrs.put("zimbraAutoProvAttrMap", "userPassword=userPassword");
/* 737 */     Domain domain = createZimbraDomain(genDomainSegmentName(), domainAttrs);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 742 */     Map<String, Object> extAcct1Attrs = Maps.newHashMap();
/* 743 */     extAcct1Attrs.put("cn", "authaccount01");
/* 744 */     createExternalAcctEntry("acct1", "test123", extAcct1Attrs);
/*     */     
/* 746 */     Map<String, Object> extAcct2Attrs = Maps.newHashMap();
/* 747 */     extAcct2Attrs.put("cn", "authaccount02");
/* 748 */     createExternalAcctEntry("acct2", "test123", extAcct2Attrs);
/*     */     
/* 750 */     Map<String, Object> extAcct3Attrs = Maps.newHashMap();
/* 751 */     extAcct3Attrs.put("cn", "authaccount03");
/* 752 */     createExternalAcctEntry("acct3", "test123", extAcct3Attrs);
/*     */     
/* 754 */     Map<String, Object> extAcct4Attrs = Maps.newHashMap();
/* 755 */     extAcct4Attrs.put("cn", "authaccount04");
/* 756 */     createExternalAcctEntry("acct4", "test123", extAcct4Attrs);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 761 */     SoapTransport transport = authZimbraAdmin();
/* 762 */     DomainSelector domainSel = new DomainSelector(DomainSelector.DomainBy.name, domain.getName());
/* 763 */     PrincipalSelector principalSel = PrincipalSelector.create(AutoProvPrincipalBy.name, "authaccount04");
/* 764 */     AutoProvAccountRequest req = AutoProvAccountRequest.create(domainSel, principalSel);
/*     */     
/* 766 */     boolean caughtException = false;
/*     */     try {
/* 768 */       invokeJaxb(transport, req);
/*     */     } catch (ServiceException e) {
/* 770 */       String msg = e.getMessage();
/*     */       
/* 772 */       if (e.getCode().equals("ldap.MULTIPLE_ENTRIES_MATCHED")) if (msg.contains(String.format("uid=acct1,ou=people,%s", new Object[] { extDomainDn }))) if (msg.contains(String.format("uid=acct2,ou=people,%s", new Object[] { extDomainDn }))) if (msg.contains(String.format("uid=acct3,ou=people,%s", new Object[] { extDomainDn }))) if (msg.contains(String.format("uid=acct4,ou=people,%s", new Object[] { extDomainDn })))
/*     */               {
/*     */ 
/*     */ 
/*     */ 
/* 777 */                 caughtException = true;
/*     */               }
/*     */     }
/* 780 */     Assert.assertTrue(caughtException);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 785 */     domain.setAutoProvLdapSearchFilter("(cn=%n)");
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 790 */     AutoProvAccountResponse resp = (AutoProvAccountResponse)invokeJaxb(transport, req);
/* 791 */     AccountInfo acctInfo = resp.getAccount();
/* 792 */     Assert.assertEquals(TestUtil.getAddress("authaccount04", domain.getName()), acctInfo.getName());
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 797 */     caughtException = false;
/*     */     try {
/* 799 */       invokeJaxb(transport, req);
/*     */     } catch (ServiceException e) {
/* 801 */       String msg = e.getMessage();
/*     */       
/* 803 */       if (e.getCode().equals("account.ACCOUNT_EXISTS")) {
/* 804 */         caughtException = true;
/*     */       }
/*     */     }
/* 807 */     Assert.assertTrue(caughtException);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/soap/TestAutoProvision.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */